package org.igs.android.ogl.engine.model.a3ds;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.microedition.khronos.opengles.GL10;

import org.igs.android.ogl.engine.AndromedaException;
import org.igs.android.ogl.engine.Renderer;
import org.igs.android.ogl.engine.math.Point3f;
import org.igs.android.ogl.engine.scene.SceneObject;

/**
 * 
 * This class is used to store the model data which is created by 3DS MAX.
 * 
 */
public final class Model extends SceneObject {

    // number of objects
    int numOfObjects;
    
    // number of material
    int numOfMaterials;
    
    // vectors keeping the materials
    public ArrayList<tMaterialInfo> Materials = new ArrayList<tMaterialInfo>();
    
    // vectors keeping the objects
    public ArrayList<ModelObject> _3Dobjects = new ArrayList<ModelObject>();

    public Model(String id, SceneObject parent, Renderer renderer, Point3f position, Point3f rotation, Point3f scale) {
    	super(id, parent, renderer, position, rotation, scale);
    }

    /**
     * This function would generate all the buffer for render according those
     * data has been imported .
     */
    public void generate() throws AndromedaException {
    	for (ModelObject object : _3Dobjects) {
            if (object.materialID >= 0) {
            	try {
                    String strFilename = Materials.get(object.materialID).strFile;
                    if (strFilename != null && !strFilename.equals(""))     
                    {
                            InputStream is = super.getRenderer().getContext().getAssets().open(strFilename);
                            DataInputStream dis = new DataInputStream(is);
                            object.loadBitmap(super.getRenderer().getGL10(), dis);
                    }
	            } catch (IOException e) {
	                    e.printStackTrace();
	            }
	            object.createBuffer();
            }
        }
    }

    public ModelObject getObject(String objectName) {
        for(ModelObject _3Dobject : _3Dobjects){
            if (_3Dobject.strName.equals(objectName)) {
                return _3Dobject;
            }
        }
        return null;
    }
    
    
    @Override
    protected void finalize() throws Throwable {
        super.finalize();
    }
    
    /**
     * This function would render all objects in the model with the buffer that
     * has been generated by function generate().
     */
	@Override
	public void render(float delta) {
		GL10 gl = super.getRenderer().getGL10();
		this.begin();
	        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
	        //gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
	        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	        this.applyTransformation();
	        for(ModelObject _3Dobject : _3Dobjects){
	            _3Dobject.draw(gl);
	        }
	        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	        //gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
	        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	    this.end();
	}

}

/**
 *  This class is used to keep the material data.
 */
class tMaterialInfo 
{       
        public String strName;                          // name of material
        public String strFile;                          // texture's filename
        public byte[]  color;                           // RGB color
        
        public tMaterialInfo() {
                color = new byte[3];
        }
        
}